package com.xinchuang.service.actual.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinchuang.comment.R;
import com.xinchuang.domain.pojo.TryOutDeviceMod;
import com.xinchuang.mapper.TryOutDeviceModMapper;
import com.xinchuang.service.actual.TryOutDeviceModService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinchuang.utils.HanyuPinyinUtil;
import com.xinchuang.utils.MyUtils;
import com.xinchuang.utils.ObjectUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zx
 * @since 2023-08-04
 */
@Service
public class TryOutDeviceModServiceImpl extends ServiceImpl<TryOutDeviceModMapper, TryOutDeviceMod> implements TryOutDeviceModService {

    @Override
    public String getDeviceList(JSONObject jsonObject) {
        //jsonobject解析获取规格型号
        String serialNumber = jsonObject.getString("serialNumber");
        //jsonobject解析获取页码
        Long pageNum = jsonObject.getLong("pageNum");
        //jsonobject解析获取每页数量
        Long pageSize = jsonObject.getLong("pageSize");
        Page<TryOutDeviceMod> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<TryOutDeviceMod> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(serialNumber)) {
            wrapper.eq(TryOutDeviceMod::getSerialNumber, serialNumber);
        }
        IPage<TryOutDeviceMod> resultList = page(page, wrapper);
        List<Map<String, Object>> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(resultList.getRecords())) {
            Map<String, Object> map;
            for (TryOutDeviceMod record : resultList.getRecords()) {
                map = new HashMap<>();
                map.put("serialNumber", MyUtils.getString(record.getSerialNumber()));
                map.put("price", MyUtils.getDouble(record.getPrice()));
                map.put("deviceName", MyUtils.getString(record.getDeviceName()));
                map.put("deviceNamePinYin", map.get("deviceName") + "");
                map.put("deviceId", MyUtils.getString(record.getDeviceId()));
                map.put("timeOutPirce", MyUtils.getString(record.getTimeOutPirce()));
                map.put("normalTimePrice", MyUtils.getString(record.getNormalTimePrice()));
                list.add(map);
            }
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("totalNum", resultList.getTotal());
            resultMap.put("dataList", list);
            return JSON.toJSONString(R.data(resultMap));
        } else {
            return JSON.toJSONString(R.success("无数据"));
        }
    }

    @Override
    public String addDevice(JSONObject jsonObject) {
        //jsonobject解析获取试模设备型号
        String deviceName = jsonObject.getString("deviceName");
        //jsonobject解析获取规格型号
        String serialNumber = jsonObject.getString("serialNumber");
        //jsonobject解析获取标准时间单价
        Double price = jsonObject.getDouble("price");
        //jsonobject解析获取标准时间单价
        String normalTimePrice = jsonObject.getString("normalTimePrice");
        //jsonobject解析获取超出标准时间单价
        String timeOutPirce = jsonObject.getString("timeOutPirce");
        TryOutDeviceMod deviceMod = new TryOutDeviceMod();
        deviceMod.setDeviceName(deviceName);
        deviceMod.setSerialNumber(serialNumber);
        deviceMod.setPrice(price);
        deviceMod.setNormalTimePrice(normalTimePrice);
        deviceMod.setTimeOutPirce(timeOutPirce);
        if (save(deviceMod)) {
            return JSON.toJSONString(R.success("添加成功"));
        }
        return JSON.toJSONString(R.fail("添加失败"));
    }

    @Override
    public String saveDevice(JSONObject jsonObject) {
        //jsonobject解析获取试模设备型号
        String deviceName = jsonObject.getString("deviceName");
        //jsonobject解析获取规格型号
        String serialNumber = jsonObject.getString("serialNumber");
        //jsonobject解析获取标准时间单价
        Double price = jsonObject.getDouble("price");
        //jsonobject解析获取标准时间单价
        String normalTimePrice = jsonObject.getString("normalTimePrice");
        //jsonobject解析获取设备id
        String deviceId = jsonObject.getString("deviceId");
        //jsonobject解析获取超出标准时间单价
        String timeOutPirce = jsonObject.getString("timeOutPirce");
        TryOutDeviceMod deviceMod = getById(deviceId);
        if (ObjectUtil.isEmpty(deviceMod)) {
            return JSON.toJSONString(R.fail("数据不存在/已删除"));
        }
        deviceMod.setDeviceName(deviceName);
        deviceMod.setSerialNumber(serialNumber);
        deviceMod.setPrice(price);
        deviceMod.setNormalTimePrice(normalTimePrice);
        deviceMod.setTimeOutPirce(timeOutPirce);
        updateById(deviceMod);
        return JSON.toJSONString(R.success("保存成功"));
    }

    @Override
    public String deleteDevice(JSONObject jsonObject) {
        //jsonobject解析获取设备Id
        String deviceId = jsonObject.getString("deviceId");
        if (removeById(deviceId)) {
            return JSON.toJSONString(R.success("删除成功"));
        }
        return JSON.toJSONString(R.success("无数据"));
    }

    @Override
    public String queryDeviceByName(JSONObject jsonObject) {
        //jsonobject解析获取试模设备型号
        String deviceName = jsonObject.getString("deviceName");
        String deviceId = jsonObject.getString("deviceId");
        LambdaQueryWrapper<TryOutDeviceMod> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(deviceName), TryOutDeviceMod::getDeviceName, deviceName);
        wrapper.eq(StringUtils.isNotBlank(deviceId), TryOutDeviceMod::getDeviceId, deviceId);
        List<TryOutDeviceMod> deviceMods = list(wrapper);
        List<Map<String, Object>> list = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(deviceMods)) {
            Map<String, Object> map = new HashMap<>();
            for (TryOutDeviceMod record : deviceMods) {
                map.put("serialNumber", MyUtils.getString(record.getSerialNumber()));
                map.put("price", MyUtils.getDouble(record.getPrice()));
                map.put("normalTimePrice", MyUtils.getString(record.getNormalTimePrice()));
                map.put("deviceName", MyUtils.getString(record.getDeviceName()));
                map.put("deviceId", MyUtils.getString(record.getDeviceId()));
                map.put("timeOutPirce", MyUtils.getString(record.getTimeOutPirce()));
                list.add(map);
            }
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("dataList", list);
            return JSON.toJSONString(R.data(resultMap));
        }
        return JSON.toJSONString(R.success("无数据"));
    }
}
